package com.mybatis.dialog;

import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.openapi.project.Project;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.intellij.util.TextFieldCompletionProvider;
import com.intellij.util.textCompletion.TextFieldWithCompletion;
import com.mybatis.handler.BaseMethodNameHandler;
import com.mybatis.utils.JavaUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class MethodNameForSqlDialog {
    private final Project project;
    private final List<String> methodNames;
    private TextFieldWithCompletion textFieldWithCompletion;
    private JPanel mainPanel;

    public MethodNameForSqlDialog(@NotNull Project project, String value, List<String> methodNames) {
        this.project = project;
        this.methodNames = methodNames;
        $$$setupUI$$$();
        this.textFieldWithCompletion.setText(value);
    }

    private void createUIComponents() {
        this.textFieldWithCompletion = new TextFieldWithCompletion(this.project, new TextFieldCompletionProvider() {
            @Override
            protected void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result) {
                BaseMethodNameHandler baseMethodNameHandler = JavaUtils.buildMethodName(text, prefix, methodNames);
                JavaUtils.completionAddElements(JavaUtils.buildNewCompletionResult(result, prefix, baseMethodNameHandler.getMethodNames()), baseMethodNameHandler, project, textFieldWithCompletion.getEditor(), CompletionType.CLASS_NAME);
            }
        }, "", true, true, true);
    }

    public JPanel getPanel() {
        return this.mainPanel;
    }

    public TextFieldWithCompletion getTextFieldWithCompletion() {
        return this.textFieldWithCompletion;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
        mainPanel.add(textFieldWithCompletion, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        mainPanel.add(spacer1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mainPanel;
    }

}
